package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.CureMedicineDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskMedicineDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTypeDAO;
import cn.edu.scau.cmi.hombio.dao.PreventionTypeDAO;
import cn.edu.scau.cmi.hombio.dao.UnitDAO;

import cn.edu.scau.cmi.hombio.domain.CureMedicine;
import cn.edu.scau.cmi.hombio.domain.CureTaskMedicine;
import cn.edu.scau.cmi.hombio.domain.FodderType;
import cn.edu.scau.cmi.hombio.domain.PreventionType;
import cn.edu.scau.cmi.hombio.domain.Unit;

import cn.edu.scau.cmi.hombio.service.UnitService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Unit entities
 * 
 */

@Controller("UnitRestController")

public class UnitRestController {

	/**
	 * DAO injected by Spring that manages CureMedicine entities
	 * 
	 */
	@Autowired
	private CureMedicineDAO cureMedicineDAO;

	/**
	 * DAO injected by Spring that manages CureTaskMedicine entities
	 * 
	 */
	@Autowired
	private CureTaskMedicineDAO cureTaskMedicineDAO;

	/**
	 * DAO injected by Spring that manages FodderType entities
	 * 
	 */
	@Autowired
	private FodderTypeDAO fodderTypeDAO;

	/**
	 * DAO injected by Spring that manages PreventionType entities
	 * 
	 */
	@Autowired
	private PreventionTypeDAO preventionTypeDAO;

	/**
	 * DAO injected by Spring that manages Unit entities
	 * 
	 */
	@Autowired
	private UnitDAO unitDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Unit entities
	 * 
	 */
	@Autowired
	private UnitService unitService;

	/**
	 * Create a new Unit entity
	 * 
	 */
	@RequestMapping(value = "/Unit", method = RequestMethod.POST)
	@ResponseBody
	public Unit newUnit(@RequestBody Unit unit) {
		unitService.saveUnit(unit);
		return unitDAO.findUnitByPrimaryKey(unit.getId());
	}

	/**
	* View an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/preventionTypesForReportUnitId/{preventiontype_id}", method = RequestMethod.GET)
	@ResponseBody
	public PreventionType loadUnitPreventionTypesForReportUnitId(@PathVariable Integer unit_id, @PathVariable Integer related_preventiontypesforreportunitid_id) {
		PreventionType preventiontype = preventionTypeDAO.findPreventionTypeByPrimaryKey(related_preventiontypesforreportunitid_id, -1, -1);

		return preventiontype;
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* View an existing FodderType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/fodderTypes/{foddertype_id}", method = RequestMethod.GET)
	@ResponseBody
	public FodderType loadUnitFodderTypes(@PathVariable Integer unit_id, @PathVariable Integer related_foddertypes_id) {
		FodderType foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(related_foddertypes_id, -1, -1);

		return foddertype;
	}

	/**
	* Save an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/Unit", method = RequestMethod.PUT)
	@ResponseBody
	public Unit saveUnit(@RequestBody Unit unit) {
		unitService.saveUnit(unit);
		return unitDAO.findUnitByPrimaryKey(unit.getId());
	}

	/**
	* View an existing CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/cureMedicines/{curemedicine_id}", method = RequestMethod.GET)
	@ResponseBody
	public CureMedicine loadUnitCureMedicines(@PathVariable Integer unit_id, @PathVariable Integer related_curemedicines_id) {
		CureMedicine curemedicine = cureMedicineDAO.findCureMedicineByPrimaryKey(related_curemedicines_id, -1, -1);

		return curemedicine;
	}

	/**
	* Delete an existing CureTaskMedicine entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/cureTaskMedicines/{curetaskmedicine_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteUnitCureTaskMedicines(@PathVariable Integer unit_id, @PathVariable Integer related_curetaskmedicines_id) {
		unitService.deleteUnitCureTaskMedicines(unit_id, related_curetaskmedicines_id);
	}

	/**
	* Delete an existing FodderType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/fodderTypes/{foddertype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteUnitFodderTypes(@PathVariable Integer unit_id, @PathVariable Integer related_foddertypes_id) {
		unitService.deleteUnitFodderTypes(unit_id, related_foddertypes_id);
	}

	/**
	* Delete an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/preventionTypesForReportUnitId/{preventiontype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteUnitPreventionTypesForReportUnitId(@PathVariable Integer unit_id, @PathVariable Integer related_preventiontypesforreportunitid_id) {
		unitService.deleteUnitPreventionTypesForReportUnitId(unit_id, related_preventiontypesforreportunitid_id);
	}

	/**
	* Save an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/preventionTypesForUnitId", method = RequestMethod.PUT)
	@ResponseBody
	public PreventionType saveUnitPreventionTypesForUnitId(@PathVariable Integer unit_id, @RequestBody PreventionType preventiontypesforunitid) {
		unitService.saveUnitPreventionTypesForUnitId(unit_id, preventiontypesforunitid);
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontypesforunitid.getId());
	}

	/**
	* Show all Unit entities
	* 
	*/
	@RequestMapping(value = "/Unit", method = RequestMethod.GET)
	@ResponseBody
	public List<Unit> listUnits() {
		return new java.util.ArrayList<Unit>(unitService.loadUnits());
	}

	/**
	* Create a new CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/cureMedicines", method = RequestMethod.POST)
	@ResponseBody
	public CureMedicine newUnitCureMedicines(@PathVariable Integer unit_id, @RequestBody CureMedicine curemedicine) {
		unitService.saveUnitCureMedicines(unit_id, curemedicine);
		return cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicine.getId());
	}

	/**
	* Create a new CureTaskMedicine entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/cureTaskMedicines", method = RequestMethod.POST)
	@ResponseBody
	public CureTaskMedicine newUnitCureTaskMedicines(@PathVariable Integer unit_id, @RequestBody CureTaskMedicine curetaskmedicine) {
		unitService.saveUnitCureTaskMedicines(unit_id, curetaskmedicine);
		return cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(curetaskmedicine.getId());
	}

	/**
	* Create a new FodderType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/fodderTypes", method = RequestMethod.POST)
	@ResponseBody
	public FodderType newUnitFodderTypes(@PathVariable Integer unit_id, @RequestBody FodderType foddertype) {
		unitService.saveUnitFodderTypes(unit_id, foddertype);
		return fodderTypeDAO.findFodderTypeByPrimaryKey(foddertype.getId());
	}

	/**
	* Create a new PreventionType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/preventionTypesForReportUnitId", method = RequestMethod.POST)
	@ResponseBody
	public PreventionType newUnitPreventionTypesForReportUnitId(@PathVariable Integer unit_id, @RequestBody PreventionType preventiontype) {
		unitService.saveUnitPreventionTypesForReportUnitId(unit_id, preventiontype);
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype.getId());
	}

	/**
	* Show all FodderType entities by Unit
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/fodderTypes", method = RequestMethod.GET)
	@ResponseBody
	public List<FodderType> getUnitFodderTypes(@PathVariable Integer unit_id) {
		return new java.util.ArrayList<FodderType>(unitDAO.findUnitByPrimaryKey(unit_id).getFodderTypes());
	}

	/**
	* Delete an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/preventionTypesForUnitId/{preventiontype_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteUnitPreventionTypesForUnitId(@PathVariable Integer unit_id, @PathVariable Integer related_preventiontypesforunitid_id) {
		unitService.deleteUnitPreventionTypesForUnitId(unit_id, related_preventiontypesforunitid_id);
	}

	/**
	* Show all CureMedicine entities by Unit
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/cureMedicines", method = RequestMethod.GET)
	@ResponseBody
	public List<CureMedicine> getUnitCureMedicines(@PathVariable Integer unit_id) {
		return new java.util.ArrayList<CureMedicine>(unitDAO.findUnitByPrimaryKey(unit_id).getCureMedicines());
	}

	/**
	* Create a new PreventionType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/preventionTypesForUnitId", method = RequestMethod.POST)
	@ResponseBody
	public PreventionType newUnitPreventionTypesForUnitId(@PathVariable Integer unit_id, @RequestBody PreventionType preventiontype) {
		unitService.saveUnitPreventionTypesForUnitId(unit_id, preventiontype);
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontype.getId());
	}

	/**
	* Show all CureTaskMedicine entities by Unit
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/cureTaskMedicines", method = RequestMethod.GET)
	@ResponseBody
	public List<CureTaskMedicine> getUnitCureTaskMedicines(@PathVariable Integer unit_id) {
		return new java.util.ArrayList<CureTaskMedicine>(unitDAO.findUnitByPrimaryKey(unit_id).getCureTaskMedicines());
	}

	/**
	* Save an existing CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/cureMedicines", method = RequestMethod.PUT)
	@ResponseBody
	public CureMedicine saveUnitCureMedicines(@PathVariable Integer unit_id, @RequestBody CureMedicine curemedicines) {
		unitService.saveUnitCureMedicines(unit_id, curemedicines);
		return cureMedicineDAO.findCureMedicineByPrimaryKey(curemedicines.getId());
	}

	/**
	* Delete an existing CureMedicine entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/cureMedicines/{curemedicine_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteUnitCureMedicines(@PathVariable Integer unit_id, @PathVariable Integer related_curemedicines_id) {
		unitService.deleteUnitCureMedicines(unit_id, related_curemedicines_id);
	}

	/**
	* Save an existing FodderType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/fodderTypes", method = RequestMethod.PUT)
	@ResponseBody
	public FodderType saveUnitFodderTypes(@PathVariable Integer unit_id, @RequestBody FodderType foddertypes) {
		unitService.saveUnitFodderTypes(unit_id, foddertypes);
		return fodderTypeDAO.findFodderTypeByPrimaryKey(foddertypes.getId());
	}

	/**
	* Show all PreventionType entities by Unit
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/preventionTypesForReportUnitId", method = RequestMethod.GET)
	@ResponseBody
	public List<PreventionType> getUnitPreventionTypesForReportUnitId(@PathVariable Integer unit_id) {
		return new java.util.ArrayList<PreventionType>(unitDAO.findUnitByPrimaryKey(unit_id).getPreventionTypesForReportUnitId());
	}

	/**
	* Save an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/preventionTypesForReportUnitId", method = RequestMethod.PUT)
	@ResponseBody
	public PreventionType saveUnitPreventionTypesForReportUnitId(@PathVariable Integer unit_id, @RequestBody PreventionType preventiontypesforreportunitid) {
		unitService.saveUnitPreventionTypesForReportUnitId(unit_id, preventiontypesforreportunitid);
		return preventionTypeDAO.findPreventionTypeByPrimaryKey(preventiontypesforreportunitid.getId());
	}

	/**
	* View an existing PreventionType entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/preventionTypesForUnitId/{preventiontype_id}", method = RequestMethod.GET)
	@ResponseBody
	public PreventionType loadUnitPreventionTypesForUnitId(@PathVariable Integer unit_id, @PathVariable Integer related_preventiontypesforunitid_id) {
		PreventionType preventiontype = preventionTypeDAO.findPreventionTypeByPrimaryKey(related_preventiontypesforunitid_id, -1, -1);

		return preventiontype;
	}

	/**
	* Show all PreventionType entities by Unit
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/preventionTypesForUnitId", method = RequestMethod.GET)
	@ResponseBody
	public List<PreventionType> getUnitPreventionTypesForUnitId(@PathVariable Integer unit_id) {
		return new java.util.ArrayList<PreventionType>(unitDAO.findUnitByPrimaryKey(unit_id).getPreventionTypesForUnitId());
	}

	/**
	* Delete an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteUnit(@PathVariable Integer unit_id) {
		Unit unit = unitDAO.findUnitByPrimaryKey(unit_id);
		unitService.deleteUnit(unit);
	}

	/**
	* Save an existing CureTaskMedicine entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/cureTaskMedicines", method = RequestMethod.PUT)
	@ResponseBody
	public CureTaskMedicine saveUnitCureTaskMedicines(@PathVariable Integer unit_id, @RequestBody CureTaskMedicine curetaskmedicines) {
		unitService.saveUnitCureTaskMedicines(unit_id, curetaskmedicines);
		return cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(curetaskmedicines.getId());
	}

	/**
	* Select an existing Unit entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}", method = RequestMethod.GET)
	@ResponseBody
	public Unit loadUnit(@PathVariable Integer unit_id) {
		return unitDAO.findUnitByPrimaryKey(unit_id);
	}

	/**
	* View an existing CureTaskMedicine entity
	* 
	*/
	@RequestMapping(value = "/Unit/{unit_id}/cureTaskMedicines/{curetaskmedicine_id}", method = RequestMethod.GET)
	@ResponseBody
	public CureTaskMedicine loadUnitCureTaskMedicines(@PathVariable Integer unit_id, @PathVariable Integer related_curetaskmedicines_id) {
		CureTaskMedicine curetaskmedicine = cureTaskMedicineDAO.findCureTaskMedicineByPrimaryKey(related_curetaskmedicines_id, -1, -1);

		return curetaskmedicine;
	}
}